package com.xust.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xust.oa.domain.dto.DepartmentDTO;
import com.xust.oa.domain.entity.TAdmin;
import com.xust.oa.domain.entity.TDepartment;
import com.xust.oa.domain.vo.DepartmentVO;
import com.xust.oa.mapper.TDepartmentMapper;
import com.xust.oa.service.TDepartmentService;
import com.xust.oa.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZHANGJIE
 * @description 针对表【t_department】的数据库操作Service实现
 * @createDate 2025-03-25 22:24:35
 */
@Service
public class TDepartmentServiceImpl extends ServiceImpl<TDepartmentMapper, TDepartment>
        implements TDepartmentService {


    @Autowired
    TDepartmentMapper departmentMapper;

    @Override
    public Result<List<DepartmentVO>> searchList(DepartmentDTO departmentDTO) {
        Integer pageNum = departmentDTO.getPageNum();
        if (pageNum == null || pageNum < 0) pageNum = 0;
        Integer pageSize = departmentDTO.getPageSize();
        if (pageSize == null || pageSize < 1) pageSize = 10;
        Page<TDepartment> page = new Page<>(pageNum, pageSize);

        QueryWrapper<TDepartment> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(departmentDTO.getName()))
            queryWrapper.like("name", departmentDTO.getName());
        if (!StringUtils.isEmpty(departmentDTO.getEnabled()))
            queryWrapper.eq("enabled", departmentDTO.getEnabled());

        // 执行分页查询
        IPage<TDepartment> pageResult = departmentMapper.selectPage(page, queryWrapper);
        // 获取分页结果中的数据列表
        List<TDepartment> list = pageResult.getRecords();
        List<DepartmentVO> departmentVOList = BeanUtil.copyToList(list, DepartmentVO.class);
        return Result.ok(departmentVOList);
    }
}
